package net.facelib.sdk;

import java.util.Arrays;

import gu.jimgutil.MatrixUtils;
import net.gdface.image.MatType;
import net.gdface.utils.Assert;

public class ImageSupport {

	private ImageSupport() {
	}

	/**
	 * 图像矩阵的颜色空间转换为BGR
	 * @param matType 矩阵类型:RGB,BGR,RGBA,NV21
	 * @param matData 图像矩阵
	 * @param width 矩阵宽度
	 * @param height 矩阵高度
	 * @return NV21 图像矩阵
	 */
	public static byte[] toNV21(MatType matType,byte[] matData, int width, int height){
		Assert.notNull(matType, "matType");
		Assert.notNull(matData, "matData");
		switch (matType) {
		case NV21:
			return matData;
		case RGB:
			return MatrixUtils.RGB2NV21(matData, width, height);
		case BGR:
			return MatrixUtils.BGR2NV21(matData, width, height);
		case RGBA:
			return MatrixUtils.RGBA2NV21(matData, width, height);
		default:
			throw new UnsupportedOperationException("UNSUPPORTED MATRIX TYPE: " + matType);
		}
	}

	/**
	 * 图像矩阵的颜色空间转换为BGR
	 * @param matType 矩阵类型:RGB,BGR,RGBA,NV21
	 * @param matData 图像矩阵
	 * @param width 矩阵宽度
	 * @param height 矩阵高度
	 * @return BGR 图像矩阵
	 */
	public static byte[] toBGR(MatType matType,byte[] matData, int width, int height){
		Assert.notNull(matType, "matType");
		Assert.notNull(matData, "matData");
		switch (matType) {
		case NV21:
			return MatrixUtils.NV212BGR(matData, width, height);
		case RGB:
			return MatrixUtils.RGB2BGR(matData, width, height,width*3);
		case BGR:
			return matData;
		case RGBA:
			return MatrixUtils.RGBA2BGR(matData, width, height,width*4);
		default:
			throw new UnsupportedOperationException("UNSUPPORTED MATRIX TYPE: " + matType);
		}
	}

	/**
	 * 图像矩阵的颜色空间转换为RGB
	 * @param matType 矩阵类型:RGB,BGR,RGBA,NV21
	 * @param matData 图像矩阵
	 * @param width 矩阵宽度
	 * @param height 矩阵高度
	 * @return RGB 图像矩阵
	 */
	public static byte[] toRGB(MatType matType,byte[] matData, int width, int height){
		Assert.notNull(matType, "matType");
		Assert.notNull(matData, "matData");
		switch (matType) {
		case NV21:
			return MatrixUtils.NV212RGB(matData, width, height);
		case RGB:
			return matData;			
		case BGR:
			return MatrixUtils.BGR2RGB(matData, width, height,width*3);
		case RGBA:
			return MatrixUtils.RGBA2RGB(matData, width, height,width*4);
		default:
			throw new UnsupportedOperationException("UNSUPPORTED MATRIX TYPE: " + matType);
		}
	}

	/**
	 * 图像矩阵的颜色空间转换为RGBA
	 * @param matType 矩阵类型:RGB,BGR,RGBA,NV21
	 * @param matData 图像矩阵
	 * @param width 矩阵宽度
	 * @param height 矩阵高度
	 * @return RGBA 图像矩阵
	 */
	public static byte[] toRGBA(MatType matType,byte[] matData, int width, int height){
		Assert.notNull(matType, "matType");
		Assert.notNull(matData, "matData");
		switch (matType) {
		case NV21:
			return MatrixUtils.NV212RGBA(matData, width, height);
		case RGB:
			return MatrixUtils.RGB2RGBA(matData, width, height,width*4);
		case BGR:
			return MatrixUtils.BGR2RGBA(matData, width, height,width*3);
		case RGBA:
			return matData;			
		default:
			throw new UnsupportedOperationException("UNSUPPORTED MATRIX TYPE: " + matType);
		}
	}
	/**
	 * 图像矩阵的颜色空间转换为灰度图像
	 * @param matType 矩阵类型:RGB,BGR,RGBA,NV21
	 * @param matData 图像矩阵
	 * @param width 矩阵宽度
	 * @param height 矩阵高度
	 * @return RGBA 图像矩阵
	 */
	public static byte[] toGRAY(MatType matType,byte[] matData, int width, int height){
		Assert.notNull(matType, "matType");
		Assert.notNull(matData, "matData");
		switch (matType) {
		case NV21:
			return Arrays.copyOf(matData, width*height);
		case RGB:
			return MatrixUtils.RGB2GRAY(matData, width, height,width*3);
		case BGR:	
			return MatrixUtils.RGB2GRAY(toRGB(matType, matData, width, height), width, height,width*3);
		case RGBA:
			return MatrixUtils.RGBA2GRAY(matData, width, height,width*4);
		case GRAY:
			return matData;			
		default:
			throw new UnsupportedOperationException("UNSUPPORTED MATRIX TYPE: " + matType);
		}
	}
}
